﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Domain.IRepository.SiteLibraryManage;
using YunSpace.Repository.SiteLibraryManage;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Domain.Entity;
using YunSpace.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
#endregion

namespace YunSpace.Application.SiteLibraryManage
{
    public class SiteDemandApp
    {
        private ISiteDemandRepository service = new SiteDemandRepository();

        public List<SiteDemandEntity> GetList(Pagination pagination, string queryJson)
        {
            var result = new List<SiteDemandEntity>();
            using (var db = new RepositoryBase())
            {
                var strSql = @"select * from SL_SiteDemand si where si.F_DeleteMark <>1 {0}{1}{2}{3}{4}{5}{6} ";
                var queryParam = queryJson.ToJObject();
                var userid = "";
                if (!queryParam["userid"].IsEmpty()) {
                    var currentUser = OperatorProvider.Provider.GetCurrent();
                    userid = currentUser.UserId;
                }

                DbParameter[] parameter =
                {
                    new MySqlParameter("@state", queryParam["IsProcessed"]),
                    new MySqlParameter("@site_type", queryParam["SiteType"]),
                    new MySqlParameter("@provinces_id", queryParam["ProvincesId"]),
                    new MySqlParameter("@city", queryParam["city"]),
                    new MySqlParameter("@district", queryParam["DistrictId"]),
                    new MySqlParameter("@business_area", queryParam["BusinessArea"]),
                    new MySqlParameter("@userid", userid),

                };

                if (!queryParam.IsEmpty())
                {
                    string isDBUserWhere = "";
                    if (!queryParam["userid"].IsEmpty()) {
                        if (queryParam["userid"].ToString() == "1")
                        {
                            isDBUserWhere = " and si.F_BDUserId = @userid";
                        }
                        else {
                            isDBUserWhere = " and si.F_CreatorUserId = @userid";
                        }
                    }
                    strSql = string.Format(strSql, 
                        queryParam["IsProcessed"].IsEmpty() ? "" : " and si.F_IsProcessed =@state ",
                        queryParam["SiteType"].IsEmpty() ? "" : " and si.F_SiteType=@site_type ",
                        queryParam["ProvincesId"].IsEmpty() ? "" : " and si.F_ProvincesId=@provinces_id ",
                        queryParam["city"].IsEmpty() ? "" : " and si.F_CityId=@city ",
                        queryParam["DistrictId"].IsEmpty() ? "" : " and si.F_DistrictId=@district ",
                        queryParam["BusinessArea"].IsEmpty() ? "" : " and si.F_BusinessArea = @business_area",
                        userid.IsEmpty() ? "" : isDBUserWhere);
                }
                result = db.FindList<SiteDemandEntity>(strSql, parameter,pagination);
            }
            return result;
        }

        public SiteDemandEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void UpdateState(string keyValue) {
            SiteDemandEntity sitedemandEntity = GetForm(keyValue);
            sitedemandEntity.Modify(keyValue);
            sitedemandEntity.F_IsProcessed = true;
            service.Update(sitedemandEntity);
        }
        public void SubmitForm(SiteDemandEntity sitedemandEntity, string keyValue)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                var ProvincesIds = sitedemandEntity.F_ProvincesId;
                var CityIds = sitedemandEntity.F_CityId;
                var DistrictIds = sitedemandEntity.F_DistrictId;
                var BusinessAreas = sitedemandEntity.F_BusinessArea;
                for (int i = 0; i < ProvincesIds.Split(',').Length; i++)
                {
                    var ProvincesId = ProvincesIds.Split(',')[i];
                    var CityId = CityIds.Split(',')[i];
                    var DistrictId = DistrictIds.Split(',')[i];
                    var BusinessArea = BusinessAreas.Split(',')[i];

                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        sitedemandEntity.F_ProvincesId = ProvincesId;
                        sitedemandEntity.F_CityId = CityId;
                        sitedemandEntity.F_DistrictId = DistrictId;
                        sitedemandEntity.F_BusinessArea = BusinessArea;
                        sitedemandEntity.Modify(keyValue);
                        service.Update(sitedemandEntity);
                    }
                    else
                    {
                        sitedemandEntity.Create();
                        sitedemandEntity.F_ProvincesId = ProvincesId;
                        sitedemandEntity.F_CityId = CityId;
                        sitedemandEntity.F_DistrictId = DistrictId;
                        sitedemandEntity.F_BusinessArea = BusinessArea;
                        sitedemandEntity.F_DeleteMark = false;
                        sitedemandEntity.F_IsProcessed = false;
                        sitedemandEntity.F_LastModifyTime = sitedemandEntity.F_CreatorTime;
                        sitedemandEntity.F_LastModifyUserId = sitedemandEntity.F_LastModifyUserId;
                        service.Insert(sitedemandEntity);
                    }
                }

                if (!string.IsNullOrEmpty(keyValue))
                {
                    if (sitedemandEntity.F_IsProcessed==true)
                    {
                        MessageNoticeEntity message = new MessageNoticeEntity();
                        message.Create();
                        message.F_IsRead = 0;
                        message.F_MessageType = 2;
                        message.F_MessageWay = 1;
                        message.F_ReceiveUserId = sitedemandEntity.F_CreatorUserId;
                        message.F_MessageContent = "您发起的场地拓展需求已处理完成！";
                        db.Insert<MessageNoticeEntity>(message);
                    }
                }
                else
                {
                    MessageNoticeEntity message = new MessageNoticeEntity();
                    message.Create();
                    message.F_IsRead = 0;
                    message.F_MessageType = 2;
                    message.F_MessageWay = 1;
                    message.F_ReceiveUserId = sitedemandEntity.F_BDUserId;
                    message.F_MessageContent = "您有新的场地拓展需求";
                    db.Insert<MessageNoticeEntity>(message);
                }
                db.Commit();
            }
        }
    }
}

